package dji.ux.ext.widget.gimbal;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import dji.common.error.DJIError;
import dji.common.util.CommonCallbacks;
import dji.thirdparty.io.reactivex.android.schedulers.AndroidSchedulers;
import dji.ux.beta.R;
import dji.ux.beta.base.ConstraintLayoutWidget;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;

public class DualGimbalWidget extends ConstraintLayoutWidget {

    private DualGimbalWidgetModel widgetModel;
    private ImageView imageView;

    public DualGimbalWidget(@NonNull Context context) {
        super(context);
    }

    public DualGimbalWidget(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DualGimbalWidget(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        inflate(context, R.layout.ext_widget_dual_gimbal, this);
        setBackgroundColor(R.drawable.ext_bg_black_round_corner);

        widgetModel = new DualGimbalWidgetModel(DJISDKModel.getInstance(),
                ObservableInMemoryKeyedStore.getInstance());

        imageView = findViewById(R.id.iv_dual_gimbal);
        imageView.setOnClickListener(view -> widgetModel.setDualGimbalSwitch(new CommonCallbacks.CompletionCallbackWith<Boolean>() {
            @Override
            public void onSuccess(Boolean aBoolean) {
                if (aBoolean != null) {
                    imageView.setBackgroundResource((aBoolean ? R.drawable.ext_ic_dual_gimbal_open : R.drawable.ext_ic_dual_gimbal_closed));
                } else {
                    setVisibility(GONE);
                }
            }

            @Override
            public void onFailure(DJIError djiError) {
                setVisibility(GONE);
            }
        }));
    }

    @Override
    protected void reactToModelChanges() {
        addReaction(widgetModel.getProductConnection()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::onProductConnect));
    }

    @NonNull
    @Override
    public String getIdealDimensionRatioString() {
        return null;
    }

    private void onProductConnect(Boolean isConnected) {
        if (isConnected && widgetModel.isSupportedDualGimbal()) {
            setVisibility(VISIBLE);
            updateDualGimbalState();
        } else {
            setVisibility(GONE);
        }
    }

    private void updateDualGimbalState() {
        widgetModel.getDualGimbalSwitchOn(new CommonCallbacks.CompletionCallbackWith<Boolean>() {
            @Override
            public void onSuccess(Boolean aBoolean) {
                if (aBoolean != null) {
                    imageView.setBackgroundResource((aBoolean ? R.drawable.ext_ic_dual_gimbal_open : R.drawable.ext_ic_dual_gimbal_closed));
                } else {
                    setVisibility(GONE);
                }
            }

            @Override
            public void onFailure(DJIError djiError) {
                setVisibility(GONE);
            }
        });
    }
}
